'use strict'
const path = require('path')
// const defaultSettings = require('./src/settings')
// 换肤插件
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const forElementUI = require('webpack-theme-color-replacer/forElementUI')

function resolve(dir) {
  return path.join(__dirname, dir)
}

// 网页标题
// const name = defaultSettings.title || '千行开发框架' // page title

// 服务端口号 port = 8080
// npm run dev OR npm run dev --port = 8080
const port = process.env.port || process.env.npm_config_port || 7521 // dev port

// https://cli.vuejs.org/zh/config/
module.exports = {
  pages: {
    index: {
      entry: ['node_modules/babel-polyfill', 'src/main.js'],
      template: 'public/index.html',
      filename: 'index.html'
    }
  },
  publicPath: './',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    // open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/gc-starter-server': {
        target: `http://192.168.52.93:8081/gc-starter-server`,
        // target: `http://localhost:8081/gc-starter-server`,
        changeOrigin: true,
        pathRewrite: {
          '^/gc-starter-server': ''
        }
      }
    }
    // proxy: {
    //   // change xxx-api/login => mock/login
    //   // detail: https://cli.vuejs.org/config/#devserver-proxy
    //   [process.env.VUE_APP_BASE_API]: {
    //     target: `http://127.0.0.1:${port}/mock`,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       ['^' + process.env.VUE_APP_BASE_API]: ''
    //     }
    //   }
    // },
    // after: require('./mock/mock-server.js')
  },
  css: {
    // 向所有 Sass 样式传入全局变量
    // loaderOptions: {
    //   sass: {
    //     // @ 是 src 的别名
    //     data: `@import "gc-template/packages/assets/styles/theme-default/common/gc-var.scss";`
    //   }
    // },
    extract: false
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    // name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      // build css which contains theme colors
      new ThemeColorReplacer({
        fileName: 'static/css/theme-colors.[contenthash:8].css',
        matchColors: forElementUI.getElementUISeries('#007aff'),
        changeSelector: forElementUI.changeSelector
      })
    ]
  },
  chainWebpack(config) {
    config.entry('main').add('babel-polyfill')
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('./node_modules/gc-starter-ui-plus/packages/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('./node_modules/gc-starter-ui-plus/packages/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    // config
    //   .when(process.env.NODE_ENV !== 'development',
    //     config => {
    //       config
    //         .plugin('ScriptExtHtmlWebpackPlugin')
    //         .after('html')
    //         .use('script-ext-html-webpack-plugin', [{
    //         // `runtime` must same as runtimeChunk name. default is `runtime`
    //           inline: /runtime\..*\.js$/
    //         }])
    //         .end()
    //       config
    //         .optimization.splitChunks({
    //           chunks: 'all',
    //           cacheGroups: {
    //             libs: {
    //               name: 'chunk-libs',
    //               test: /[\\/]node_modules[\\/]/,
    //               priority: 10,
    //               chunks: 'initial' // only package third parties that are initially dependent
    //             },
    //             elementUI: {
    //               name: 'chunk-elementUI', // split elementUI into a single package
    //               priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
    //               test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
    //             },
    //             commons: {
    //               name: 'chunk-commons',
    //               test: resolve('src/components'), // can customize your rules
    //               minChunks: 3, //  minimum common number
    //               priority: 5,
    //               reuseExistingChunk: true
    //             }
    //           }
    //         })
    //       // config.optimization.runtimeChunk('single')
    //     }
    //   )
  },
  // 默认情况下 babel-loader 会忽略所有 node_modules 中的文件。如果你想要通过 Babel 显式转译一个依赖，可以在这个选项中列出来。
  transpileDependencies: [
    'gc-starter-ui-plus'
    // 'element-ui',
    // 'axios',
    // 'vue-router-tab',
    // 'vue-cookie'
  ]
}
